package com.kzd.sdk.ui.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.core.KZD_ApiConstance;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.core.KZD_SdkController;
import com.kzd.sdk.entity.KZD_UserAgree;
import com.kzd.sdk.ui.base.WebViewActivity;
import com.kzd.sdk.utils.AppUtils;

import static com.kzd.sdk.core.KZD_ApiConstance.KEY_WEB_VIEW_CON;
import static com.kzd.sdk.core.KZD_ApiConstance.KEY_WEB_VIEW_TITLE;
import static com.kzd.sdk.core.KZD_ApiConstance.KEY_WEB_VIEW_TYPE;
import static com.kzd.sdk.core.KZD_ApiConstance.KEY_WEB_VIEW_URL;

public class KZD_WebRichActivity extends WebViewActivity {

    public static void openApi(String title, String type) {
        Activity activity = KZD_SdkController.getActivity();
        if (activity == null) {
            return;
        }
        Intent intent = new Intent(activity, KZD_WebRichActivity.class);
        intent.putExtra(KEY_WEB_VIEW_TITLE, title);
        intent.putExtra(KEY_WEB_VIEW_TYPE, type);
        activity.startActivity(intent);
    }

    public static void openCon(String title, String content) {
        Activity activity = KZD_SdkController.getActivity();
        if (activity == null) {
            return;
        }
        Intent intent = new Intent(activity, KZD_WebRichActivity.class);
        intent.putExtra(KEY_WEB_VIEW_TITLE, title);
        intent.putExtra(KEY_WEB_VIEW_CON, content);
        activity.startActivity(intent);
    }

    public static void openUrl(String title, String url) {
        Activity activity = KZD_SdkController.getActivity();
        if (activity == null) {
            return;
        }
        Intent intent = new Intent(activity, KZD_WebRichActivity.class);
        intent.putExtra(KEY_WEB_VIEW_TITLE, title);
        intent.putExtra(KEY_WEB_VIEW_URL, url);
        activity.startActivity(intent);
    }

    @SuppressLint("AddJavascriptInterface")
    @Override
    public void initView() {
        mWebView.addJavascriptInterface((this), ("android"));
    }

    @Override
    public void initData() {
        String title = getIntent().getStringExtra(KEY_WEB_VIEW_TITLE);
        if (TextUtils.isEmpty(title)) {
            setTabLayoutGone();
        } else {
            setTextViewTitle(title);
        }

        String type = getIntent().getStringExtra(KEY_WEB_VIEW_TYPE);
        if (!TextUtils.isEmpty(type)) {
            KZD_ApiManager.httpUserAgree(type, new KZD_HttpCallback<KZD_UserAgree>() {
                @Override
                public void onSuccess(KZD_UserAgree data) {
                    mWebView.loadDataWithBaseURL((null), data.getContent(), ("text/html"), ("utf-8"), (null));
                }

                @Override
                public void onError(String error) {
                    toast(error);
                }
            });
        }

        String content = getIntent().getStringExtra(KEY_WEB_VIEW_CON);
        if (!TextUtils.isEmpty(content)) {
            mWebView.loadDataWithBaseURL((null), content, ("text/html"), ("utf-8"), (null));
        }

        String url = getIntent().getStringExtra(KEY_WEB_VIEW_URL);
        if (!TextUtils.isEmpty(url)) {
            mWebView.loadUrl(url);
        }
    }

    @JavascriptInterface
    public void finishActivity() {
        finish();
    }

    @JavascriptInterface
    public void openGTGameApp() {
        if (!AppUtils.isAppInstalled((this), KZD_ApiConstance.GT_GAME_PACKAGE)) {
            AppUtils.launchBrowser((this), KZD_ApiConstance.GT_GAME_WEB);
        } else {
            AppUtils.launchApp((this), KZD_ApiConstance.GT_GAME_PACKAGE);
        }
    }

    @JavascriptInterface
    public void exitLogin(int type){
        if (type == KZD_ApiConstance.LOGIN_MAIN) {
            KZD_SdkController.getOnHideListener().onAccountSwitch();
            KZD_SdkController.getOnLoginListener().onLoginSwitch(KZD_ApiConstance.LOGIN_MAIN);
            KZD_AccessActivity.open();
            finish();
        }
        if (type == KZD_ApiConstance.LOGIN_SMALL) {
            KZD_SdkController.getOnLoginListener().onLoginSwitch(KZD_ApiConstance.LOGIN_SMALL);
        }
    }
}
